#include "StdAfx.h"
#include "DataTransmitMag.h"

#include "LogWrapper.h"

CDataTransmitMag::CDataTransmitMag(void)
{
	m_hModule = NULL;
	m_lpCreateSenderHelper = NULL;
	m_lpDestroySenderHelper= NULL;
}

CDataTransmitMag::~CDataTransmitMag(void)
{
	this->Destroy();
}

bool CDataTransmitMag::Initialize()
{
#ifndef NOT_USE_DATA_TRANSMIT_DLL
#ifdef _WIN64
	m_hModule = LoadLibrary(L"CTDataTransmit64.dll");
#else
	m_hModule = LoadLibrary(L"CTDataTransmit.dll");
#endif
	DWORD dwError = GetLastError();
	if (dwError != 0 && !m_hModule)
	{
		CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, L"load CTDataTransmit failed. Error code is %d.\n", dwError);
		return false;
	}
	m_lpCreateSenderHelper = (LPCreateSenderHelper)GetProcAddress(m_hModule, "CreateSenderHelper");
	dwError = GetLastError();
	if (!m_lpCreateSenderHelper)
	{
		CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, L"get CreateSenderHelper interface failed. Error code is %d.\n", dwError);
		return false;
	}
	m_lpDestroySenderHelper= (LPDestroySenderHelper)GetProcAddress(m_hModule, "DestroySenderHelper");
	dwError = GetLastError();
	if (!m_lpDestroySenderHelper)
	{
		CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, L"get DestroySenderHelper interface failed. Error code is %d.\n", dwError);
		return false;
	}
#endif
	return true;
}

void CDataTransmitMag::Destroy()
{
	if (m_hModule)
	{
		FreeLibrary(m_hModule);
		m_lpCreateSenderHelper = NULL;
		m_lpDestroySenderHelper= NULL;
	}
}

IDataSenderHelper* CDataTransmitMag::CreateSenderHelper()
{
	if (m_lpCreateSenderHelper)
	{
		return (*m_lpCreateSenderHelper)();
	}
	else
	{
		return NULL;
	}
}

bool CDataTransmitMag::DestroySenderHelper( IDataSenderHelper* helper)
{
	if (m_lpDestroySenderHelper)
	{
		return (*m_lpDestroySenderHelper)(helper);
	}
	else
	{
		return false;
	}
}